package com.kyrie.lib_sys.application.categoryApplys.controller;

import com.google.common.base.Preconditions;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.entity.Result;
import com.kyrie.lib_sys.common.pojo.dto.CategoryDTO;
import com.kyrie.lib_sys.common.pojo.dto.CategoryPageQueryDTO;
import com.kyrie.lib_sys.application.categoryApplys.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类相关controller类
 *
 * @author heefM
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/category/")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/add")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("添加分类Controller:addCategory: {}", categoryDTO);
        }
        Preconditions.checkArgument(!StringUtils.isBlank(categoryDTO.getName()), "分类名称不能为空");

        categoryService.insert(categoryDTO);
        return Result.success("添加分类成功");
    }

    /**
     * 根据id查询分类
     *
     * @param id
     * @return
     */
    @RequestMapping("/queryById")
    public Result<CategoryDTO> queryCategoryById(@RequestParam Long id) {
        log.info("根据id查询分类: {}", id);
        Preconditions.checkNotNull(id, "分类id不能为空");
        CategoryDTO categoryDTO = categoryService.queryCategoryById(id);

        return Result.success(categoryDTO);
    }

    /**
     * 分页查询所有分类
     *
     * @return
     */
    @RequestMapping("/list")
    public Result<PageResult> queryPage(CategoryPageQueryDTO categoryPageQueryDTO) {

        log.info("分页查询所有分类: {}", categoryPageQueryDTO);
        // 设置默认分页参数
        if (categoryPageQueryDTO.getPageNum() == null) {
            categoryPageQueryDTO.setPageNum(1);
        }
        if (categoryPageQueryDTO.getPageSize() == null) {
            categoryPageQueryDTO.setPageSize(10);
        }
        PageResult pageResult = categoryService.queryPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 条件查询分类
     *
     * @param categoryDTO
     * @return
     */
    @RequestMapping("/queryByCondition")
    public Result<CategoryDTO> queryByCondition(@RequestBody CategoryDTO categoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("条件查询分类: {}", categoryDTO);
        }
        categoryDTO = categoryService.queryByCondition(categoryDTO);
        return Result.success(categoryDTO);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     * @return
     */
    @RequestMapping("/update")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        if (log.isInfoEnabled()) {
            log.info("修改分类Controller:updateCategory: {}", categoryDTO);
        }
        Preconditions.checkArgument(categoryDTO != null, "分类信息不能为空");
        categoryService.update(categoryDTO);
        return Result.success("修改分类成功");
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result<String> deleteCategory(@RequestParam("id") Long id) {
        if (log.isInfoEnabled()) {
            log.info("删除分类Controller:deleteCategory: {}", id);
        }
        categoryService.deleteById(id);
        return Result.success("删除分类成功");
    }

    /**
     * 批量删除分类
     *
     * @param ids
     * @return
     */
    @RequestMapping("/deleteBatch")
    public Result<String> deleteBatch(@RequestParam List<Long> ids) {
        if (log.isInfoEnabled()) {
            log.info("批量删除分类Controller:deleteBatch: {}", ids);
        }
        categoryService.deleteBatch(ids);
        return Result.success("批量删除分类成功");
    }

    /**
     * 批量新增分类
     *
     * @param categoryDTOS
     * @return
     */
    @RequestMapping("/insertBatch")
    public Result insertBatch(@RequestBody List<CategoryDTO> categoryDTOS) {
        if (log.isInfoEnabled()) {
            log.info("批量新增分类insertBatch: {}", categoryDTOS);
        }
        Preconditions.checkArgument(categoryDTOS != null && categoryDTOS.size() > 0, "分类列表不能为空");
        categoryService.insertBatch(categoryDTOS);
        return Result.success("批量新增分类成功");
    }
}
